package com.good.child.widgets.editext;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.good.child.R;

/**
 * Create on: 2024/2/27
 * by Mr.Zhu
 * Description: TODO
 */
public class XEditText  extends DigitsEditText {

    private DrawableLeftListener mLeftListener;
    private DrawableRightListener mRightListener;

    final int DRAWABLE_LEFT = 0;
    final int DRAWABLE_TOP = 1;
    final int DRAWABLE_RIGHT = 2;
    final int DRAWABLE_BOTTOM = 3;


    private CharSequence hint;
    private ColorStateList textColor;

    private boolean isFocusHighText = true;

    public XEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        textColor = getTextColors();
        addTextChangedListener(new TextWatchAdapter() {
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    setTextSize(15);
                } else {
                    setTextSize(12);
                }
            }
        });
    }

    public XEditText(Context context, AttributeSet attrs) {
        this(context, attrs, androidx.appcompat.R.attr.editTextStyle);
    }

    public XEditText(Context context) {
        this(context, null);
    }

    public void setDrawableLeftListener(DrawableLeftListener listener) {
        this.mLeftListener = listener;
    }

    public void setDrawableRightListener(DrawableRightListener listener) {
        this.mRightListener = listener;
    }

    public void setFocusHightText(boolean focusHighText) {
        isFocusHighText = focusHighText;
    }

    @Override
    public CharSequence getHint() {
        if (hint == null) {
            return hint = super.getHint();
        }
        return hint;
    }

//    @Override
//    public void setText(CharSequence text, BufferType type) {
//        super.setText(text, type);
//        if (!TextUtils.isEmpty(text)) {
//            setSelection(text.length());
//        }
//    }

    @Override
    public void setOnFocusChangeListener(OnFocusChangeListener l) {
        if (!isFocusHighText) {
            super.setOnFocusChangeListener(l);
            return;
        }
        super.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                hint = getHint();
                if (l != null)
                    l.onFocusChange(v, hasFocus);
                if (hasFocus) {
                    post(() -> {
                        setHint("");
                    });
                } else {
                    post(() -> {
                        setHint(hint);
                    });
                }
            }
        });
    }

    public interface DrawableLeftListener {
        boolean onDrawableLeftClick(View view);
    }

    public interface DrawableRightListener {
        boolean onDrawableRightClick(View view);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 触摸状态
        if (event.getAction() == MotionEvent.ACTION_UP) {
            // 监听DrawableLeft
            if (mLeftListener != null) {
                // 判断DrawableLeft是否被点击
                Drawable drawableLeft = getCompoundDrawables()[0];
                // 当按下的位置 < 在EditText的到左边间距+图标的宽度+Padding
                if (drawableLeft != null && event.getX() <= (getLeft() + getTotalPaddingLeft() + drawableLeft.getBounds().width())) {
                    // 执行DrawableLeft点击事件
                    boolean intercept = mLeftListener.onDrawableLeftClick(this);
                    if (intercept) {
                        clearFocus();

                        return true;
                    }
                }
            }
            // 监听DrawableRight
            if (mRightListener != null) {
                Drawable drawableRight = getCompoundDrawables()[2];
                // 当按下的位置 > 在EditText的到右边间距-图标的宽度-Padding
                if (drawableRight != null && event.getX() >= (getRight() - getTotalPaddingRight() - drawableRight.getBounds().width())) {
                    // 执行DrawableRight点击事件
                    boolean intercept = mRightListener.onDrawableRightClick(this);
                    if (intercept) {

                        return true;
                    }
                }
            }
        }
        return super.onTouchEvent(event);
    }
}
